package com.kld.yqyj.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.kld.common.utils.StringUtils;
import com.kld.common.core.page.TableDataInfo;
import com.kld.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.kld.yqyj.domain.bo.TWellCsBo;
import com.kld.yqyj.domain.vo.TWellCsVo;
import com.kld.yqyj.domain.TWellCs;
import com.kld.yqyj.mapper.TWellCsMapper;
import com.kld.yqyj.service.ITWellCsService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 井测试数据Service业务层处理
 *
 * @author bw
 * @date 2024-05-15
 */
@RequiredArgsConstructor
@Service
public class TWellCsServiceImpl implements ITWellCsService {

    private final TWellCsMapper baseMapper;

    /**
     * 查询井测试数据
     *
     * @param wellId 井测试数据主键
     * @return 井测试数据
     */
    @Override
    public TWellCsVo queryById(String wellId){
        return baseMapper.selectVoById(wellId);
    }

    /**
     * 查询井测试数据列表
     *
     * @param bo 井测试数据
     * @return 井测试数据
     */
    @Override
    public TableDataInfo<TWellCsVo> queryPageList(TWellCsBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TWellCs> lqw = buildQueryWrapper(bo);
        Page<TWellCsVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询井测试数据列表
     *
     * @param bo 井测试数据
     * @return 井测试数据
     */
    @Override
    public List<TWellCsVo> queryList(TWellCsBo bo) {
        LambdaQueryWrapper<TWellCs> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TWellCs> buildQueryWrapper(TWellCsBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TWellCs> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getWellName()), TWellCs::getWellName, bo.getWellName());
        lqw.eq(bo.getWellWzly() != null, TWellCs::getWellWzly, bo.getWellWzly());
        lqw.eq(bo.getWellYxhd() != null, TWellCs::getWellYxhd, bo.getWellYxhd());
        lqw.eq(bo.getWellKxd() != null, TWellCs::getWellKxd, bo.getWellKxd());
        lqw.eq(bo.getWellStl() != null, TWellCs::getWellStl, bo.getWellStl());
        lqw.eq(bo.getWellHqbhd() != null, TWellCs::getWellHqbhd, bo.getWellHqbhd());
        lqw.eq(bo.getWellYy() != null, TWellCs::getWellYy, bo.getWellYy());
        lqw.eq(bo.getWellTy() != null, TWellCs::getWellTy, bo.getWellTy());
        return lqw;
    }

    /**
     * 新增井测试数据
     *
     * @param bo 井测试数据
     * @return 结果
     */
    @Override
    public Boolean insertByBo(TWellCsBo bo) {
        TWellCs add = BeanUtil.toBean(bo, TWellCs.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setWellId(add.getWellId());
        }
        return flag;
    }

    /**
     * 修改井测试数据
     *
     * @param bo 井测试数据
     * @return 结果
     */
    @Override
    public Boolean updateByBo(TWellCsBo bo) {
        TWellCs update = BeanUtil.toBean(bo, TWellCs.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(TWellCs entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除井测试数据
     *
     * @param wellIds 需要删除的井测试数据主键
     * @return 结果
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
